package com.servlet;

import java.io.IOException;
import java.io.Writer;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.DBUtils.DBUtil;

/**
 * Servlet implementation class signInServlet
 */
@WebServlet("/signInServlet")
public class signInServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public signInServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String name=request.getParameter("userName");
		String pwd=request.getParameter("passWord");
//		System.out.println(name+"\t"+pwd);
		
		HttpSession session=request.getSession();
		
		
		Writer out=response.getWriter();
		boolean flag=false;
		Connection con=DBUtil.getconn();
		try {
			Statement stm=con.createStatement();
			String sql="select NickName,Password from bbsaccount";
			ResultSet rs=stm.executeQuery(sql);
			
			while(rs.next()){
//				System.out.println(rs.getString(1)+"\t"+rs.getString(2));
				if(rs.getString(1).equals(name)&&rs.getString(2).equals(pwd)){
					flag=true;
					break;
				}
			}
			if(flag){
				out.write("success");
				session.setAttribute("name", name);
			}else{
				out.write("fail");
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
